<template>
	<view class="u-m-20">
		<u-navbar :is-back="true" title="个人信息" :border-bottom="false"></u-navbar>
		<view>
			<u-cell-group>
				<u-cell-item title="头像" :arrow="true" hover-class="none" @click="updateAvatar">
					<u-avatar :src="pic" size="100"></u-avatar>
				</u-cell-item>
			</u-cell-group>
			<u-cell-group>
				<u-cell-item title="昵称" :arrow="true" hover-class="none" @click="updateName">
					{{vuex_user.user.nickName}}
				</u-cell-item>
			</u-cell-group>
			<u-cell-group>
				<u-cell-item title="生日" :arrow="true" hover-class="none" @click="updateAge">
					{{$u.timeFormat(vuex_user.user.age, 'yyyy年mm月dd日')}}
				</u-cell-item>
			</u-cell-group>
			<u-cell-group>
				<u-cell-item title="党龄" :arrow="true" hover-class="none" @click="updatePartyAge">
					{{$u.timeFormat(vuex_user.user.partyAge, 'yyyy年mm月dd日')}}
				</u-cell-item>
			</u-cell-group>
		</view>
		
		<u-modal v-model="showModel" @confirm="confirmName" ref="uModal" :async-close="true"
		title="设置昵称">
			<view class="slot-content">
				<u-input v-model="nickName" type="text" :border="false" placeholder="请输入昵称"/>
			</view>
		</u-modal>
		
			<u-picker v-model="showAgeModel" mode="time" @confirm="confirmAge"></u-picker>
			<u-picker v-model="showPartyAgeModel" mode="time" @confirm="confirmPartyAge"></u-picker>
		
		<!-- <view class="u-m-t-20">
			<u-button type="primary" @click="subProfile">提交</u-button>
		</view>
		
		<!-- 如果是微信登录小程序，则获取用户的昵称与头像 -->
		<!-- #ifdef MP-WEIXIN -->  
			<!-- <u-button type="default">使用微信头像与昵称</u-button> -->
		<!-- #endif -->
	</view>
</template>

<script>
	import config from "@/common/config.js" // 全局配置文件
	export default {
		data() {
			return {
				pic:uni.getStorageSync('lifeData').vuex_user.user.avatar.includes(config.staticUrl)?uni.getStorageSync('lifeData').vuex_user.user.avatar:config.staticUrl+uni.getStorageSync('lifeData').vuex_user.user.avatar,
				nickName:null,
				age:null,
				showModel:false,
				showAgeModel:false,
				showPartyAgeModel:false,
			}
		},
		methods: {
			updateName(){
				this.showModel = true;
			},
			updateAge(){
				console.log(uni.getStorageSync('lifeData').vuex_user,111)
				this.showAgeModel = true;
			},
			updatePartyAge(){
				this.showPartyAgeModel = true
			},
			confirmAge(time){
				let varTime = new Date(time.year+"-"+time.month+"-"+time.day)
				let url = "api/profile/updateProfile";
				let vuex_user = uni.getStorageSync('lifeData').vuex_user
				let user = vuex_user.user
				let userId = user.userId;
				this.$u.put(url,{
					userId:userId,
					age:varTime,
				}).then(data => {
					// 关闭
					user.age = varTime
					this.$u.vuex('vuex_user', vuex_user);
					this.showAgeModel = false;
					this.$mytip.toast('生日修改成功')
				});
			},
			confirmPartyAge(time){
				let varTime = new Date(time.year+"-"+time.month+"-"+time.day)
				let url = "api/profile/updateProfile";
				let vuex_user = uni.getStorageSync('lifeData').vuex_user
				let user = vuex_user.user
				let userId = user.userId;
				this.$u.put(url,{
					userId:userId,
					partyAge:varTime,
				}).then(data => {
					// 关闭
					user.partyAge = varTime
					this.$u.vuex('vuex_user', vuex_user);
					this.showAgeModel = false;
					this.$mytip.toast('入党时间修改成功')
				});
			},
			confirmName() {
				if(!this.nickName){
					this.showModel = false;
					return this.$mytip.toast('请输入昵称')
				}
				let url = "api/profile/updateProfile";
				let vuex_user = uni.getStorageSync('lifeData').vuex_user
				let user = vuex_user.user
				let userId = user.userId;
				this.$u.put(url,{
					userId:userId,
					nickName:this.nickName,
				}).then(data => {
					// 关闭
					user.nickName = this.nickName
					this.$u.vuex('vuex_user', vuex_user);
					this.showModel = false;
					this.$mytip.toast('昵称修改成功')
				});
			},
			updateAvatar(){
				this.$u.route('/pages/profile/avatar')
			},
			subProfile(){
				// 1.更新vuex中的用户信息
				this.$mytip.toast('修改成功')
				// 2.页面跳转
				uni.switchTab({
					url: '/pages/center/center'
				})
			},
			getUserProfile(){
				// 如果是微信登录小程序，则获取用户的昵称与头像
				// #ifdef MP-WEIXIN
					// 此处执行微信才执行
				// #endif
			    var that = this;
			    uni.getUserProfile({
			        desc: '获取您的微信信息以授权小程序',
			        lang: 'zh_CN',
			        success: UserProfileRes => {  
			            console.log(UserProfileRes)
			            uni.login({
			                provider: 'weixin',
			                success: function(loginRes) {
			                    let avatarUrl = UserProfileRes.userInfo.avatarUrl; //用户头像
			                    let nickName = UserProfileRes.userInfo.nickName; //用户微信名
			                    let gender = UserProfileRes.userInfo.gender;//性别
								console.log(avatarUrl);
								// 去修改用户的昵称与头像
			                },
			                fail(err) {
			                    console.log(err)
			                }
			            });
			        },
			        fail:err=>{
			            console.log(err)
			        }
			    })
			}
		}
	}
</script>

<style lang="scss" scoped>
	.slot-content{
		padding: 40rpx;
	}
</style>